<template>
  <div class="login-container">
    <div class="titles">
      <img src="../images/大标题-左边-20240926110947144.png">
      <div class="titles_text">安全监测云平台</div>
      <img src="../images/大标题-右边-20240926110059256.png">
    </div>
    <div class="center">
      <img class="center_left" src="../images/左侧线条动图-20240926105827209.png">
      <div class="center_center">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <img src="../images/登录边框标题背景动图-20240926110124453.png">
            <h3 class="title">用户登录</h3>
          </div>
          <div style="box-sizing: border-box; padding: 0 24px">
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="Username"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
                style="box-sizing: border-box; padding: 0 20px"
              />
            </el-form-item>

            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>

            <el-button
              :loading="loading"
              type="primary"
              style="width: 100%; margin-bottom: 10px"
              @click.native.prevent="handleLogin"
            >登录</el-button>

            <div class="tips">
              <span style="margin-right: 20px">忘记密码</span>
              <span> 账号注册</span>
            </div>
          </div>
        </el-form>
      </div>
      <img class="center_right" src="../images/右侧线条动图-20240926105849762.png">
    </div>
    <!-- <img src="../images/仅流光动效-20240930170126004.png" class="butbg" alt=""> -->
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { login, menu } from '@/api/user'
import Cookies from 'js-cookie'

// import { Message } from 'element-ui'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        this.loading = true

        if (valid) {
          login(this.loginForm).then(res => {
            if (res.code === 200) {
              this.loading = false

              // this.$router.push({ path: "dashboard" })
              console.log(this.redirect, '0000')

              this.$message('登陆成功')
              localStorage.setItem('token', res.data.token)
              Cookies.set('vue_admin_template_token', res.data.token)

              localStorage.setItem('admin_id', res.data.admin_id)

              menu().then(res => {
                if (res.code === 200) {
                  localStorage.setItem('menu', JSON.stringify(res.data))
                  this.$router.push({ path: '/' || this.redirect })
                }
              })
            } else {
              // callback(new Error(res.msg))
              this.loading = false

              this.$message(res.msg)
            }
          })
          // this.$store
          //   .dispatch('user/login', this.loginForm)
          //   .then(() => {

          //   })
          //   .catch(() => {
          //     this.loading = false
          //   })

          // this.$router.push({ path: '/' })
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.titles {
  width: 80%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  margin-top: 100px;
  .titles_text {
    font-size: 46px;
    font-weight: 500;
    // color: #fff;
    font-size: 48px;
    font-weight: bold;
    background: -webkit-linear-gradient(top, #fff, rgb(108, 195, 250));
    text-shadow: 2px 0px 10px rgb(108, 195, 250);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-fill-color: transparent;
  }

  img {
    height: 40px;
    width: 20%;
    flex: 1;
  }
}
.center_center {
  width: 400px;
  height: 300px;
  background: url('../images/登录底部-20240926110109098.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  margin-top: 15vh;
}
.center {
  display: flex;
  // justify-content: ;
  justify-content: space-between;

  box-sizing: border-box;
  width: 100%;
  padding: 0 100px;
  img {
    height: 60vh;
  }
}
.butbg {
  position: absolute;
  bottom: 1%;
  width: 100%;
}
.login-container {
  height: 100vh;
  width: 100%;
  position: relative;
  background: url('../images/bg-20240930170048058.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    height: 65px;
    width: 100%;
    top: 0;
    // background-color: #fff;
    img {
      position: absolute;
      width: 106%;
      height: 102%;
      left: -3%;
      top: -19%;
      z-index: 0;
    }

    // background-size: 100% 100%;
    .title {
      position: absolute;
      top: 20%;
      left: 50%;
      transform: translate(-50%);
      color: $light_gray;
      margin: 0px auto 40px auto;
      font-weight: bold;
      font-size: 24px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
